'This is a Smalltalk representation of Dijkstra''s Dining Philosophers Problem. The solution presented here is loosely based on the one written in Little Smalltalk, presented in Tim Budd''s book "A Little Smalltalk", pp. 116-121.
"Five philosophers spend their lives eating and thinking. The philosophers share a common circular table surrounded by five chairs, each belonging to one philosopher. In the centre of the table, there is a bowl of rice and the table is laid with five chopsticks. When a philosopher thinks, he does not interact with his colleagues. From time to time, a philosopher gets hungry and tries to pick up the two chopsticks that are closest to him (the chopsticks that are between him and his left and right neighbours). A philosopher may only pick up one chopstick at a time. Obviously, he cannot pick up a chopstick that is already in the hand of a neighbour. When a hungry philosopher has both his chopsticks at the same time, he eats without releasing his chopsticks. When he has finished eating, he puts down both of his chopsticks and starts thinking again."
The solution represents each chopstick as a Semaphore, using the ''wait'' and ''signal'' methods. This guarantees that no two philosophers use the same chopstick simultaneously. The solution is also asymmetric; an odd philosopher picks up his left chopstick first, then his right chopstick, while an even philosopher picks up his right chopstick first.
Each philosopher is represented by an instance of class Philosopher; the philosophers and the chopsticks are maintained in instance variables.
'!
!DiningPhilosophers methodsFor: 'accessing'!
chopSticks
"Answer with the array containing the chopsticks."
^chopSticks!
chopSticks: anArray
"Set up the chopsticks at the table."
chopSticks _ anArray!
numberDiners
"Answer with the number of diners at the table."
^numberDiners!
numberDiners: aNumber
"Set the number of diners at the table."
numberDiners _ aNumber!
philosophers
"Answer with the array containing the philosophers."
^philosophers!
philosophers: anArray
"Sets up the array containing the philosophers."
philosophers _ anArray! !
!DiningPhilosophers methodsFor: 'dining'!
dine: time
"The table representing the dining philosophers eating a